namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 列出所有域名解析记录命令
    /// 用于显示系统中所有已配置的域名解析记录
    /// 作者：huobing
    /// </summary>
    public sealed class ListRecordsCommand : BaseCommand<DnsRecordsResult>
    {
        public const string NAME = "LIST_RECORDS";
        public override string Name => NAME;
        public override string Format => "LIST_RECORDS";
        public override string Description => "列出所有域名解析记录";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) 
        { 
            error = string.Empty; 
            return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; 
        }
    }
    
    /// <summary>
    /// 域名解析记录项
    /// 作者：huobing
    /// </summary>
    public class DnsRecordItem
    {
        /// <summary>
        /// 域名
        /// </summary>
        public string? Domain { get; set; }

        /// <summary>
        /// IP地址
        /// </summary>
        public string? IpAddress { get; set; }
        
        /// <summary>
        /// 记录类型（A、AAAA、CNAME、MX、TXT等）
        /// </summary>
        public string RecordType { get; set; } = "A";

        /// <summary>
        /// TTL值
        /// </summary>
        public int Ttl { get; set; }

        /// <summary>
        /// 描述信息
        /// </summary>
        public string? Description { get; set; }
    }

    /// <summary>
    /// 列出域名解析记录命令执行结果
    /// 作者：huobing
    /// </summary>
    public class DnsRecordsResult : BaseCommandResult
    {
        /// <summary>
        /// 解析记录列表
        /// </summary>
        public List<DnsRecordItem> Records { get; set; }

        /// <summary>
        /// 记录总数
        /// </summary>
        public int TotalRecords => Records?.Count ?? 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DnsRecordsResult()
        {  
            Records = new List<DnsRecordItem>();
        }
        
        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的DNS记录信息</returns>
        public override string ToConsoleString()
        {  
            var sb = new System.Text.StringBuilder();
            sb.AppendLine($"域名解析记录列表（共{TotalRecords}条）：");
            if (Records == null || Records.Count == 0)
            {
                sb.AppendLine("暂无解析记录");
            }
            else
            {
                // 准备表格数据
                var tableData = new List<string[]>();
                tableData.Add(new[] { "域名", "IP地址", "记录类型", "TTL", "描述" }); // 表头
                
                foreach (var record in Records)
                {
                    tableData.Add(new[]
                    {
                        record.Domain ?? "-",
                        record.IpAddress ?? "-",
                        record.RecordType ?? "-",
                        record.Ttl.ToString(),
                        record.Description ?? "无"
                    });
                }
                
                // 使用TextTableFormatter格式化表格
                string table = Formatting.TextTableFormatter.FormatAlignedTable(tableData, ' ', 80);
                sb.AppendLine(table);
            }
            
            return sb.ToString().TrimEnd();
        }
    }
}